package com.qiyi.player.user.impls
{
   import flash.events.EventDispatcher;
   import com.qiyi.player.user.IUser;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.base.rpc.RemoteObjectEvent;
   import com.qiyi.player.base.rpc.RemoteObjectStatusEnum;
   import com.qiyi.player.user.UserDef;
   import com.qiyi.player.user.UserManagerEvent;
   import com.qiyi.player.base.logging.Log;
   
   public class User extends EventDispatcher implements IUser
   {
      
      public function User(param1:String, param2:String) {
         this._log = Log.getLogger("com.qiyi.player.user.impls.User");
         super();
         this._passportID = param1;
         this._P00001 = param2;
      }
      
      private var _id:String;
      
      private var _passportID:String;
      
      private var _P00001:String;
      
      private var _nickname:String;
      
      private var _type:int;
      
      private var _level:int;
      
      private var _userCheckRemote:UserCheckRemote;
      
      private var _log:ILogger;
      
      public function get passportID() : String {
         return this._passportID;
      }
      
      public function get P00001() : String {
         return this._P00001;
      }
      
      public function get nickName() : String {
         return this._nickname;
      }
      
      public function get id() : String {
         return this._id;
      }
      
      public function get type() : int {
         return this._type;
      }
      
      public function get level() : int {
         return this._level;
      }
      
      public function checkUser() : void {
         if(this._userCheckRemote)
         {
            this._userCheckRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onCheckResult);
            this._userCheckRemote.destroy();
         }
         this._userCheckRemote = new UserCheckRemote(this._passportID,this._P00001);
         this._userCheckRemote.addEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onCheckResult);
         this._userCheckRemote.initialize();
      }
      
      public function destroy() : void {
         if(this._userCheckRemote)
         {
            this._userCheckRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onCheckResult);
            this._userCheckRemote.destroy();
            this._userCheckRemote = null;
         }
      }
      
      private function onCheckResult(param1:RemoteObjectEvent) : void {
         if(this._userCheckRemote.status == RemoteObjectStatusEnum.Success)
         {
            if(this._userCheckRemote.userLevel == UserDef.USER_LEVEL_NORMAL)
            {
               this._id = "";
               this._nickname = "";
               this._level = UserDef.USER_LEVEL_NORMAL;
               this._type = UserDef.USER_TYPE_QIYI;
            }
            else if(this._userCheckRemote.userLevel == UserDef.USER_LEVEL_PRIMARY)
            {
               this._id = this._userCheckRemote.userID;
               this._nickname = this._userCheckRemote.userName;
               this._level = this._userCheckRemote.userLevel;
               this._type = this._userCheckRemote.userType;
            }
            
         }
         else if(this._userCheckRemote.status == RemoteObjectStatusEnum.Processing)
         {
            return;
         }
         
         this._userCheckRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onCheckResult);
         this._userCheckRemote.destroy();
         this._userCheckRemote = null;
         this._log.info("check user complete,userID:" + this._id + ", userName:" + this._nickname + ", userLevel:" + this._level + ", userType:" + this._type);
         dispatchEvent(new UserManagerEvent(UserManagerEvent.Evt_LoginSuccess));
      }
   }
}
